import { defineConfig } from "vite";

import vue from "@vitejs/plugin-vue";

import autoprefixer from "autoprefixer";

import WindiCSS from "vite-plugin-windicss";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
import path from "path";
// https://vitejs.dev/config/
import { viteMockServe } from "vite-plugin-mock";

export default defineConfig({
  base: "./",
  resolve: {
    alias: {
      "@": path.resolve(process.cwd(), "src"),
    },
  },
  css: {
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: ["Chrome > 40", "ff > 31", "ie 11"],
        }),
      ],
    },
  },
  plugins: [
    vue(),
    WindiCSS(),
    AutoImport({
      imports: [
        {
          "naive-ui": ["useMessage"],
        },
      ],
    }),
    Components({
      resolvers: [NaiveUiResolver()],
    }),
    viteMockServe({
      mockPath: "./mock", // 解析，路径可根据实际变动
      localEnabled: true, // 是否应用于本地
      prodEnabled: false, // 是否应用于生产
      watchFiles: true, // 监视文件更改 这样更改mock的时候，不需要重新启动编译
    }),
  ],
  build: {
    chunkSizeWarningLimit: 1500,
    terserOptions: {
      compress: {
        //生产环境时移除console
        drop_console: true,
        drop_debugger: true,
      },
    },
    // 重点这个配置
    rollupOptions: {
      output: {
        manualChunks: {
          /* vue是随便取的名称 */
          vue: ["vue"],
          naiveui: ["naive-ui"],
        },
      },
    },
  },
  server: {
    port: 8848,
    hmr: {
      overlay: false,
    },
    proxy: {
      // 将请求代理到另一个服务器
      "/api": {
        target: "http://192.168.99.99:8091",
        changeOrigin: true, //开启跨域
        // rewrite: (path) => path.replace(/^\/mj/, ""),
      },
    },
  },
});
